package com.example.neteasecloudmusic_kotlin.ui.homepage

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.neteasecloudmusic_kotlin.R
import com.example.neteasecloudmusic_kotlin.logic.model.HomePageEntity.*
import com.example.neteasecloudmusic_kotlin.logic.model.HomePageEntity
import com.example.neteasecloudmusic_kotlin.logic.model.NavIconEntity
import com.example.neteasecloudmusic_kotlin.ui.utils.ScrollLinearLayoutManager
import com.youth.banner.Banner
import com.youth.banner.indicator.CircleIndicator
import kotlinx.android.synthetic.main.discovery_banner.*
import kotlinx.android.synthetic.main.fragment_discovery.*

class DiscoverFragment : Fragment() {

    private val viewModel by lazy { ViewModelProvider(this).get(DiscoveryViewModel::class.java) }

    private val navIconList = ArrayList<NavIconEntity>()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_discovery, container, false)
    }

    @SuppressLint("FragmentLiveDataObserve")
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initNavIconList()
        val layoutManager = LinearLayoutManager(activity)
        layoutManager.orientation = LinearLayoutManager.HORIZONTAL
        rvNavIcon.layoutManager = layoutManager
        val adapter = NavIconAdapter(navIconList)
        rvNavIcon.adapter = adapter

        viewModel.discoveryData.observe(activity!!, Observer { result ->
            val homepage = result.getOrNull()
            if (homepage == null) {
                Toast.makeText(activity, "无法获得首页信息", Toast.LENGTH_SHORT).show()
            } else {
                val banners = homepage.rootBean?.get(0)?.banners
                val banner = (bannerLayout as Banner<String, ImageAdapter>)
                banner.apply {
                    addBannerLifecycleObserver(activity)
                    setBannerRound(20f)
                    indicator = CircleIndicator(activity)
                    setAdapter(ImageAdapter(bannersToList(banners)))
                }

                val playListInfo = ArrayList<RootBean>()
                for (i in 1 until homepage.rootBean!!.size) {
                    playListInfo.add(homepage.rootBean[i])
                }
                val playListLayoutManager = ScrollLinearLayoutManager(activity!!)
                playListLayoutManager.orientation = LinearLayoutManager.VERTICAL
                playListLayoutManager.setmCanVerticalScroll(false)
                rvDiscoveryPlayList.layoutManager = playListLayoutManager
                rvDiscoveryPlayList.adapter = DiscoveryPlayListAdapter(playListInfo, activity!!)
            }


//            swiperRefresh.isRefreshing = false
        })


        refreshDiscoveryInfo()
//        swiperRefresh.setOnRefreshListener {
//            refreshDiscoveryInfo()
//        }
    }

    private fun initNavIconList() {
        navIconList.add(NavIconEntity("每日推荐", R.drawable.ic_daily_recommed))
        navIconList.add(NavIconEntity("私人FM", R.drawable.ic_presonal_fm))
        navIconList.add(NavIconEntity("歌单", R.drawable.ic_song_list))
        navIconList.add(NavIconEntity("排行榜", R.drawable.ic_rank))
        navIconList.add(NavIconEntity("直播", R.drawable.ic_live))
        navIconList.add(NavIconEntity("唱房", R.drawable.ic_sing))
    }

    private fun bannersToList(banners: List<HomePageEntity.Banner>?) : MutableList<String> {
        var list : MutableList<String> = mutableListOf<String>()
        for (banner in banners!!) {
            list.add(banner.pic!!)
        }
        return list
    }

    private fun refreshDiscoveryInfo() {
        viewModel.refreshHomePage()
//        swiperRefresh.isRefreshing = true
    }

}